/***********************************************************************/
/*                                                                     */
/*   svm_classify.c                                                    */
/*                                                                     */
/*   Classification module of Support Vector Machine.                  */
/*                                                                     */
/*   Author: Thorsten Joachims                                         */
/*   Date: 02.07.02                                                    */
/*                                                                     */
/*   Copyright (c) 2002  Thorsten Joachims - All rights reserved       */
/*                                                                     */
/*   This software is available for non-commercial use only. It must   */
/*   not be modified and distributed without prior permission of the   */
/*   author. The author is not responsible for implications from the   */
/*   use of this software.                                             */
/*                                                                     */
/************************************************************************/

# include "SVMClassifier.h"

SVMClassifier::SVMClassifier()
{
	m_model=NULL;
}

SVMClassifier::SVMClassifier(const SVMClassifier &obj)
{
	
}

SVMClassifier::~SVMClassifier()
{
	if( m_model !=NULL)
	{
		free_model(m_model,1);
		m_model=NULL;
	}
	
}



double SVMClassifier::classify(std::string label, std::string  feature)
{
	return classify( label + "\t" + feature );
}
double SVMClassifier::classify(std::string  feature)
{
//	char *line,*comment;
	int j;
	DOC *doc;
	char *comment;
	long queryid,slackid, wnum;
	double dist=0.0,doc_label,costfactor;
	std::string line = feature;

	//parse_document(line.c_str(),m_words,&doc_label,&queryid,&slackid,&costfactor,&wnum, max_words_doc,&comment);
	parse_document((char*)line.c_str(),m_words,&doc_label,&queryid,&slackid,&costfactor,&wnum, SVM_LIGHT_MAX_WORDS,&comment);

	if(m_model->kernel_parm.kernel_type == 0)
	{
		/* Check if feature numbers   */
		for(j=0;(m_words[j]).wnum != 0;j++) 
		{
			/* are not larger than in model     */
			if((m_words[j]).wnum > m_model->totwords)
			{
				(m_words[j]).wnum=0; // Remove feature if ecessary.
			}
			doc = create_example(-1,0,0,0.0,create_svector(m_words,comment,1.0));
			dist=classify_example_linear(m_model,doc);
			free_example(doc,1);
		}
	}
	else /* non-linear kernel */
	{
		doc = create_example(-1,0,0,0.0,create_svector(m_words,comment,1.0));
		dist=classify_example(m_model,doc);
		free_example(doc,1);
	}


	return dist;
}

void SVMClassifier::loadModel(std::string fileName)
{	
	if( m_model !=NULL)
	{
		free_model(m_model,1);
		m_model=NULL;
	}
	m_model = read_model((char*) fileName.c_str() );


	/* linear kernel */
	if( m_model->kernel_parm.kernel_type == 0) 
	{
		/* compute weight vector */
		add_weight_vector_to_linear_model( m_model );
	}

	printf( "%s is loaded for svm_light\n", fileName.c_str());

}
void SVMClassifier::setParamters(long int *verbosity, long int *pred_format )
{
	*verbosity=2;
	*pred_format=1;
}

void SVMClassifier::print_help(void)
{
  printf("\nSVM-light %s: Support Vector Machine, classification module     %s\n",VERSION,VERSION_DATE);
  copyright_notice();
  printf("   usage: svm_classify [options] example_file model_file output_file\n\n");
  printf("options: -h         -> this help\n");
  printf("         -v [0..3]  -> verbosity level (default 2)\n");
  printf("         -f [0,1]   -> 0: old output format of V1.0\n");
  printf("                    -> 1: output the value of decision function (default)\n\n");
}




